package com.whz.builder;

/**
 * @Author: sinosoft
 * @Description: 建造者模式
 * @Date: Create in 19:42 2021/4/27
 * @Modified By:
 */
public class NutritionFacts {
    /*必写*/
    private final int servingSize;
    /*必写*/
    private final int servings;
    /*可选*/
    private final int calorees;
    /*可选*/
    private final int fat;
    /*可选*/
    private final int sodium;
    /*可选*/
    private final int carbohydrate;

    public static class Builder {
        /*必填参数*/
        private final int servingSize;
        private final int servings;
        /*可选参数 - 初始默认值*/
        private int calories = 0;
        private int fat = 0;
        private int sodium = 0;
        private int carbohydrate = 0;

        public Builder(int servingSize, int servings) {
            this.servingSize = servingSize;
            this.servings = servings;
        }

        public Builder calories(int calories) {
            this.calories = calories;
            return this;
        }
        public Builder fat(int fat) {
            this.fat = fat;
            return this;
        }
        public Builder sodium(int sodium) {
            this.sodium = sodium;
            return this;
        }
        public Builder carbohydrate(int carbohydrate) {
            this.carbohydrate = carbohydrate;
            return this;
        }

        public NutritionFacts builder() {
            return new NutritionFacts(this);
        }
    }

    private NutritionFacts(Builder builder) {
        servingSize = builder.servingSize;
        servings = builder.servings;
        calorees = builder.calories;
        fat = builder.fat;
        sodium = builder.sodium;
        carbohydrate = builder.carbohydrate;
    }

    @Override
    public String toString() {
        return "NutritionFacts { " +
                "servingSize=" + servingSize +
                ", servings=" + servings +
                ", calorees=" + calorees +
                ", fat=" + fat +
                ", sodium=" + sodium +
                ", carbohydrate=" + carbohydrate +
                '}';
    }
}
